/**   
* Copyright: Copyright (c) 2015 xlz
* 
* @ClassName: BaseServiceImpl.java
* @Description: 基础业务处理类
*
* @version: v1.0.0
* @author: xuyiping
* @date: 2015-4-21 上午9:37:24 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2014-1-13     xuyiping           v1.0.0               修改原因
*/
package com.byxy.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.byxy.dao.BaseDAO;
import com.byxy.service.BaseService;



/**
 * 
 * @author yiping.xu 基础业务层
 * @param <T>
 */
@Service("baseService")
@Scope("prototype")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class BaseServiceImpl implements BaseService {
	private BaseDAO baseDao;

	@Override
	public Serializable save(Object o) throws Exception {
		return getBaseDao().save(o);
	}

	@Override
	public void delete(Object o) throws Exception {
		getBaseDao().delete(o);

	}

	@Override
	public void update(Object o) throws Exception {
		getBaseDao().update(o);
	}

	@Override
	public void saveOrUpdate(Object o) throws Exception {
		getBaseDao().saveOrUpdate(o);

	}

	@Override
	public <T> T get(Class<T> c, Serializable id) throws Exception {
		return getBaseDao().get(c, id);
	}
	/**
	 * 获得一个对象
	 * 
	 * @param c
	 *            对象类型
	 * @param id
	 * @return Object
	 */
	public <T> T load(Class<T> c, Serializable id) throws Exception{
		return getBaseDao().load(c, id);
	}
	
	public BaseDAO getBaseDao() {
		return baseDao;
	}

	@Autowired
	public void setBaseDao(@Qualifier("baseDAO") BaseDAO baseDao) {
		this.baseDao = baseDao;
	}

	/**
	 * 为传入的StringBuffer对象添加where或者and
	 * 
	 * @author MaYang
	 * @param sb   传入的StringBuffer对象，为其添加where或者and
	 * @param flag 标识符，当为false时，添加where，反之添加and
	 * @throws Exception
	 */
	protected boolean addWhereOrAnd(StringBuffer sb, boolean flag) throws Exception{
		if(flag == false){
			sb.append(" where ");
		}else{
			sb.append(" and ");
		}
		return true;
	}
	
	
	
	private String subsHql(String hql){
		if(hql.substring(0, 4).equals("from")){
			return hql.substring(0);
		} else {
			return hql.substring(hql.indexOf(" from "));
		}
	}
	
	private String subsSql(String sql){
		return sql.substring(sql.indexOf(" from "));
	}
	
	
	
	
}
